#! /usr/bin/perl -w
use strict;
use warnings;

use JSON::XS;
use IO::Socket;
use constant BUFSIZE => 1024;
my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();

my @AoH = (
    {
        husband => "barney",
        wife    => "betty",
        son     => "bamm bamm",
    },
    {
        husband => "george",
        wife    => "jane",
        son     => "elroy",
    },
    {
        husband => "homer",
        wife    => "marge",
        son     => "bart",
    },
);

my $host = shift or die "Usage: client.pl host [port]\n";
my $port = shift || '10280';

my $socket = new IO::Socket(
    Domain   => PF_INET,
    PeerAddr => $host,
    PeerPort => $port,
    Proto    => getprotobyname('tcp'),
    Timeout  => 60,
) or die $@;

my $buffer;
if ( sysread( $socket, $buffer, BUFSIZE ) > 0 ) {
    syswrite( STDOUT, $buffer );
}

syswrite( $socket, $JSONObject->encode( \@AoH ), BUFSIZE );
close($socket);

